﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Contracts.Service;

namespace XcoAppSpaces.Core.CcrDispatching
{
	/// <summary>
	/// ConfigStringInterpreter for the CcrDispatcherService
	/// </summary>
	public class ConfigStringInterpreter : XcoConfigStringInterpreterBase
	{
		/// <summary>
		/// Configuration key for the ccr thread count.
		/// </summary>
		public const string Config_Ccr_Threadcount = "ccr.threadcount";

		private int? ccrThreadCount = null;

		/// <summary>
		/// Returns a new CcrDispatcherService, if the thread count has been set.
		/// </summary>
		public override IXcoService GetService
		{
			get { return (ccrThreadCount != null ? new CcrDispatcherService(ccrThreadCount.Value) : null); }
		}

		/// <summary>
		/// Action definition for interpretion
		/// </summary>
		public override Dictionary<string, Action<string>> InterpretDefinition
		{
			get
			{
				return new Dictionary<string, Action<string>> 
                    { 
                        {Config_Ccr_Threadcount, AnalyseThreadCount},//Configuration key for ccr thread counts
                    };
			}
		}

		#region analyse methods

		void AnalyseThreadCount(string ccrThreadCountStr)
		{
			int threadcount = 0;
#if PocketPC
			if (ParseHelper.TryParse(ccrThreadCountStr, out threadcount))
#else
			if (int.TryParse(ccrThreadCountStr, out threadcount))
#endif
			{
				if (threadcount < 0)
					throw new XcoConfigurationException("CCR-thread-count must be a nonnegative integer!");
				ccrThreadCount = threadcount;
			}
			else
			{
				throw new XcoConfigurationException("CCR-thread-count must be a nonnegative integer!");
			}
		}
		#endregion
	}
}
